var helper = require('../../utils/helper.js'), interval = 0, page_first_init = !0, timer = 1, fullScreen = !1, page_first = [];

Page({
  data: {
    WindowWidth: getApp().core.getSystemInfoSync().windowWidth,
    WindowHeight: getApp().core.getSystemInfoSync().windowHeight,
    signaTop:0,
    left: 0,
    home_data:[],
    goods_list:[],
    rx:[],
    // search_goods:'',
    lb:['https://assets.renrentuibao.com/web%2Fuploads%2Fimage%2Fstore_1%2F5ada916868f0ca397b4ee23497434aee8eef59ca.jpg','https://assets.renrentuibao.com/web%2Fuploads%2Fimage%2Fstore_1%2F5ada916868f0ca397b4ee23497434aee8eef59ca.jpg'],
    autoplay: true,
    circular:true,
    interval: 4000,
    duration: 500,
    show_notice: -1,
    animationData: {},
    play: -1,
    time: 0,
    buy: !1,
    opendate: !1,
    goods: "",
    form: {
      number: 1
    },
    time_all: [],
    scrollTop: {
      scroll_top: 0,
      goTop_show: false
    },
    scrollToptt:0,
    remind: false,
    ggs:2,
    pays:{},
    tolist:{},
    paged:1,
    page_count:1,
    top:0,
    bannerHeight: 0,  //banner高度
    scrollToptt: 0, //滚动条高度
    cat_id:41,
  },

  thumbsup: function (e) {
    wx.navigateTo({
      url: '/pages/tuibao-integral-mall/thumbsup/thumbsup',
    })
  },
  onLoad: function (t) {
    getApp().page.onLoad(this, t), t.page_id || (t.page_id = -1), this.setData({
      options: t
    }), this.home_page(),this.goods_list(41,this.data.paged),this.rx();
    // this.dee()
    wx.createSelectorQuery().select('#hegao').boundingClientRect((rect) => {
      this.setData({
        bannerHeight: rect.height -343
      })
    }).exec()
    let menuButton= wx.getMenuButtonBoundingClientRect()
    this.setData({
       signaTop:menuButton.top,
    })
    // this.loadData(t),
    console.log('11111122ttt',this.data)
    if (t.scene) {
      var sdsd = decodeURIComponent(t.scene);
      var sss = getApp().helper.scene_decode(sdsd);
      console.log('tttttttttttt', sss)
    }
    if (wx.getStorageSync('ACCESS_TOKEN')) {
      console.log('已登录')
      getApp().page.bindParent1(wx.getStorageSync('PARENT_ID'))
    } else {
      console.log('未登录')
      if (t.scene) {
        var recommender_id = wx.getStorageSync('recommender_id')
        var source = wx.getStorageSync('source')
        console.log('recommender_id==', recommender_id, 'source', source)
        if (recommender_id === '' || recommender_id === '0') {
          console.log('wu')
          wx.setStorageSync('recommender_id', sss.uid)
        } else {
          console.log('you')
        }
        if (source === '' || source === '0') {
          console.log('wu')
          wx.setStorageSync('source', sss.source)
        } else {
          console.log('you')
        }
      }
      if (t.user_id) {
        var recommender_id = wx.getStorageSync('recommender_id')
        console.log('recommender_id==', recommender_id)
        if (recommender_id === '' || recommender_id === '0') {
          console.log('wu')
          wx.setStorageSync('recommender_id', t.user_id)
        } else {
          console.log('you')
        }
      }
    };

  },
  search:function(e){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  cid:function(e){
    var id = e.currentTarget.dataset.cid
    this.setData({
      paged:1,
      goods_list:[],
      cat_id:id
    })
    this.goods_list(id,this.data.paged)
  },
  dee:function(e){

    getApp().request({
      url: getApp().api.surprise.exchange_index,
      data: {
        aid: '201',
      },
      success: function (t) {
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  rx:function(e){
    var  a = this
    getApp().request({
      url: getApp().api.default.goods_list,
      data:{
        cat_id:52,
      },
      success: function (t) {
        if (0 == t.code) {
          a.setData({
            rx:t.data.list
          })
        }
      }
    })
  },
  home_page:function(e){
    var that = this
    getApp().request({
      url: getApp().api.default.home_page,
      success: function (t) {
        if (0 == t.code) {
          that.setData({
            home_data:t.data
          })
        }
      }
    })
  },
  main_recommend_url:function(e){
    var a = this;
    wx.navigateTo({
      url:a.data.home_data.main_recommend_url ,
    })
  },
  recommend_first_url:function(e){
    var a = this;
    wx.navigateTo({
      url:a.data.home_data.recommend_first_url ,
    })
  },
  recommend_second_url:function(e){
    var a = this;
    wx.navigateTo({
      url:a.data.home_data.recommend_second_url ,
    })
  },
  banner_url:function(e){
    var a = this;
    wx.navigateTo({
      url:a.data.home_data.banner_url ,
    })
  },
  goods_list:function(id,paged){
    var that = this,goods_list = this.data.goods_list;
    getApp().request({
      url: getApp().api.default.goods_list,
      data:{
        cat_id:id,
        page:paged
      },
      success: function (t) {
        if (0 == t.code) {
          for(var i =0; i<t.data.list.length; i++){
            goods_list.push(t.data.list[i])
          }
          that.setData({
            goods_list:goods_list,
            page_count:t.data.page_count
          })
        }
      }
    })
  },
  populargoods:function(e){
    wx.navigateTo({
      url: '/pages/cat/cat?cat_id=52',
    })
  },
  pay_success: function (d) {
    var that = this
    getApp().request({
      url: getApp().api.order.pay_success,
      data: {
        order_id: d
      },
      success: function (t) {
        console.log('pay==',t)
        if (0 == t.code) {
          that.setData({
            pays:t.data
          })
        }
      }
    })
  },
  // suspension: function () {
  //   var o = this;
  //   interval = setInterval(function () {
  //     getApp().request({
  //       url: getApp().api.default.buy_data,
  //       data: {
  //         time: o.data.time
  //       },
  //       method: "POST",
  //       success: function (t) {
  //         if (0 == t.code) {
  //           var a = !1;
  //           o.data.msgHistory == t.md5 && (a = !0);
  //           var e = "", i = t.cha_time, s = Math.floor(i / 60 - 60 * Math.floor(i / 3600));
  //           e = 0 == s ? i % 60 + "秒" : s + "分" + i % 60 + "秒", !a && t.cha_time <= 300 ? o.setData({
  //             buy: {
  //               time: e,
  //               type: t.data.type,
  //               url: t.data.url,
  //               user: 5 <= t.data.user.length ? t.data.user.slice(0, 4) + "..." : t.data.user,
  //               avatar_url: t.data.avatar_url,
  //               address: 8 <= t.data.address.length ? t.data.address.slice(0, 7) + "..." : t.data.address,
  //               content: t.data.content
  //             },
  //             msgHistory: t.md5
  //           }) : o.setData({
  //             buy: !1
  //           });
  //         }
  //       },
  //       noHandlerFail: !0
  //     });
  //   }, 1e4);
  // },
  // loadData: function () {
  //   var i = this, t = {}, s = i.data.options;
  //   if (-1 != s.page_id) t.page_id = s.page_id; else {
  //     t.page_id = -1;
  //     var a = getApp().core.getStorageSync(getApp().const.PAGE_INDEX_INDEX);
  //     a && (a.act_modal_list = [], i.setData(a));
  //   }
  //   getApp().request({
  //     url: getApp().api.default.index,
  //     data: t,
  //     success: function (t) {
  //       if (0 == t.code) {
  //         if ("diy" == t.data.status) {
  //           var a = t.data.act_modal_list;
  //           -1 != s.page_id && (getApp().core.setNavigationBarTitle({
  //             title: t.data.info
  //           }), i.setData({
  //             title: t.data.info
  //           }));
  //           for (var e = a.length - 1; 0 <= e; e--) (void 0 === a[e].status || 0 == a[e].status) && getApp().helper.inArray(a[e].page_id, page_first) && !i.data.user_info_show || 0 == a[e].show ? a.splice(e, 1) : page_first.push(a[e].page_id);
  //           i.setData({
  //             template: t.data.template,
  //             act_modal_list: a,
  //             time_all: t.data.time_all
  //           }), i.setTime();
  //         } else page_first_init ? i.data.user_info_show || (page_first_init = !1) : t.data.act_modal_list = [],
  //           i.setData(t.data), i.miaoshaTimer();
  //         -1 == s.page_id && getApp().core.setStorageSync(getApp().const.PAGE_INDEX_INDEX, t.data);
  //       }
  //     },
  //     complete: function () {
  //       getApp().core.stopPullDownRefresh();
  //     }
  //   });
  // },

  onShow: function () {
    var e = this;
    var fordata = helper.formatData(new Date());
    console.log('shijian===', fordata)

    // this.ss()
    // if (fordata === wx.getStorageSync('fordata')) {
    //   console.log('今天以提醒')
    //   this.setData({
    //     remind: false,
    //   })
    // } else {
    //   console.log('今天未提醒')
    //   wx.setStorageSync('fordata', fordata)
    //   this.setData({
    //     remind: true,
    //     ggs:1
    //   })
    // };
    var ggs = wx.getStorageSync('ggs')
    console.log('fffffffsggggggg=',ggs)
    if(ggs == '2'){
      console.log('jinlaifffffgg')
      var order_id = wx.getStorageSync('order_id')
      this.setData({
          remind:true,
          ggs:ggs
      })
        this.pay_success(order_id)
    };
    getApp().page.onShow(this), require("./../../components/diy/diy.js").init(this),
      getApp().getConfig(function (t) {
        var a = t.store;
        a && a.name && -1 == e.data.options.page_id && getApp().core.setNavigationBarTitle({
          title: a.name
        }), a && 1 === a.purchase_frame ? e.suspension(e.data.time) : e.setData({
          buy_user: ""
        });
      }), getApp().query = null;
  },
  // onPullDownRefresh: function () {
  //   getApp().getStoreData(), clearInterval(timer), this.loadData();
  // },
  onShareAppMessage: function (t) {
    getApp().page.onShareAppMessage(this);
    var a = this, e = getApp().getUser();
    return -1 != a.data.options.page_id ? {
      path: "/pages/index/index?user_id=" + e.id + "&page_id=" + a.data.options.page_id,
      title: a.data.title
    } : {
        path: "/pages/index/index?user_id=" + e.id,
        title: a.data.store.name
      };
  },
  goods_d:function(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + id,
    })
  },
  onReachBottom:function(e){
    var a = this;
    if( a.data.paged ==  a.data.page_count){
    //   getApp().core.showToast({
    //     title: "没有更多啦",
    //     icon: "none"
    // })
    }else{
      var paged =a.data.paged
      paged += 1
      a.goods_list(a.data.cat_id,paged)
      a.setData({
        paged:paged
      })
    }
  },
  // showshop: function (t) {
  //   var a = this, e = t.currentTarget.dataset.id, i = t.currentTarget.dataset;
  //   getApp().request({
  //     url: getApp().api.default.goods,
  //     data: {
  //       id: e
  //     },
  //     success: function (t) {
  //       0 == t.code && a.setData({
  //         data: i,
  //         attr_group_list: t.data.attr_group_list,
  //         goods: t.data,
  //         showModal: !0
  //       });
  //     }
  //   });
  // },
  // miaoshaTimer: function () {
  //   var t = this;
  //   t.data.miaosha && 0 != t.data.miaosha.rest_time && (t.data.miaosha.ms_next || (timer = setInterval(function () {
  //     0 < t.data.miaosha.rest_time ? (t.data.miaosha.rest_time = t.data.miaosha.rest_time - 1,
  //       t.data.miaosha.times = t.setTimeList(t.data.miaosha.rest_time), t.setData({
  //         miaosha: t.data.miaosha
  //       })) : clearInterval(timer);
  //   }, 1e3)));
  // },
  // onHide: function () {
  //   getApp().page.onHide(this), this.setData({
  //     play: -1
  //   }), clearInterval(interval);
  // },
  // onUnload: function () {
  //   getApp().page.onUnload(this), this.setData({
  //     play: -1
  //   }), clearInterval(timer), clearInterval(interval);
  // },
  showNotice: function (t) {
    console.log(t), this.setData({
      show_notice: t.currentTarget.dataset.index
    });
  },
  closeNotice: function () {
    this.setData({
      show_notice: -1
    });
  },
  to_dial: function () {
    var t = this.data.store.contact_tel;
    getApp().core.makePhoneCall({
      phoneNumber: t
    });
  },
  luck_draw:function(e){
    wx.setStorageSync('ggs', '1')
    this.setData({
      remind: false,
    })
    wx.redirectTo({
      url: '/pages/luck-draw/luck-draw',
    })
  },
  exchange:function(e){
    this.setData({
      remind: false,
    })
    wx.setStorageSync('ggs', '1')
    wx.redirectTo({
      url: '/pages/exchange/exchange',
    })
  },
  order:function(e){
    this.setData({
      remind: false,
    })
    wx.setStorageSync('ggs', '1')
    wx.redirectTo({
      url: '/pages/order/order?status=1',
    })
  },
  closeActModal: function () {
    var a = this, e = a.data.act_modal_list;
    for (var t in e) {
      var i = parseInt(t);
      e[i].show && (e[i].show = !1);
      break;
    }
    a.setData({
      act_modal_list: e
    }), setTimeout(function () {
      for (var t in e) if (e[t].show) {
        e = e.splice(t, 1).concat(e);
        break;
      }
      a.setData({
        act_modal_list: e
      });
    }, 500);
  },
  naveClick: function (t) {
    getApp().navigatorClick(t, this);
  },
  onPageScroll: function (t) {
    var a = this;
    this.setData({
      scrollToptt: t.scrollTop
    })
    if (t.scrollTop > 300) {//触发gotop的显示条件 
      a.setData({
        'scrollTop.goTop_show': true
      });
      // console.log(this.data.scrollTop)
    } else {
      a.setData({
        'scrollTop.goTop_show': false
      });
    }
    if (!fullScreen && -1 != a.data.play) {
      var e = getApp().core.getSystemInfoSync().windowHeight;
      "undefined" == typeof my ? getApp().core.createSelectorQuery().select(".video").fields({
        rect: !0
      }, function (t) {
        (t.top <= -200 || t.top >= e - 57) && a.setData({
          play: -1
        });
      }).exec() : getApp().core.createSelectorQuery().select(".video").boundingClientRect().scrollOffset().exec(function (t) {
        (t[0].top <= -200 || t[0].top >= e - 57) && a.setData({
          play: -1
        });
      });
    }
  },
  fullscreenchange: function (t) {
    fullScreen = !!t.detail.fullScreen;
  },


  //置顶功能
  scrollTopFun: function (e) {
    console.log(e.detail);
    if (e.detail.scrollTop > 300) {//触发gotop的显示条件 
      this.setData({
        'scrollTop.goTop_show': true
      });
      // console.log(this.data.scrollTop)
    } else {
      this.setData({
        'scrollTop.goTop_show': false
      });
    }
  },
  goTopFun: function (e) {
    var _top = this.data.scrollTop.scroll_top;//发现设置scroll-top值不能和上一次的值一样，否则无效，所以这里加了个判断 
    console.log(_top)
    if (_top == 1) {
      _top = 0;
    } else {
      _top = 1;
    }
    this.setData({
      'scrollTop.scroll_top': _top
    });
  },


  remind: function (e) {
    wx.navigateTo({
      url: '/pages/free/free?remind=true' ,
  })
  },
  guanb: function (e) {
    wx.setStorageSync('ggs', '1')
    this.setData({
      remind: false,
      ggs:1
    })
  }
});